/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/itframe">itframe</a> All rights reserved.
 */
package com.rise.itframe.modules.inv.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rise.itframe.common.persistence.Page;
import com.rise.itframe.common.service.CrudService;
import com.rise.itframe.common.utils.IdGen;
import com.rise.itframe.modules.inv.dao.ReviewDao;
import com.rise.itframe.modules.inv.dao.ReviewUserDao;
import com.rise.itframe.modules.inv.entity.Review;
import com.rise.itframe.modules.inv.entity.ReviewUser;
import com.rise.itframe.modules.sys.entity.User;

/**
 * 发票登记信息Service
 * @author ming
 * @version 2017-12-31
 */
@Service
@Transactional(readOnly = true)
public class ReviewService extends CrudService<ReviewDao, Review> {
    @Autowired
    private ReviewDao reviewDao;
    @Autowired
    private ReviewUserDao reviewUserDao;
    @Override
    public Review get(String id) {
        Review review = super.get(id);
        ReviewUser reviewUser = new ReviewUser();
        reviewUser.setReviewId(id);
        review.setUserList(reviewUserDao.findList(reviewUser));
        return review;
    }

    @Override
    public List<Review> findList(Review review) {
        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
        review.getSqlMap().put("dsf", dataScopeFilter(review.getCurrentUser(), "o", "b"));
        return super.findList(review);
    }


    @Override
    public Page<Review> findPage(Page<Review> page, Review review) {
        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
        review.getSqlMap().put("dsf", dataScopeFilter(review.getCurrentUser(), "o", "b"));
        return super.findPage(page, review);
    }

    @Transactional(readOnly = false)
    public void save(Review review, String names) {

        super.save(review);
        // 保存人员
        if (org.apache.commons.lang3.StringUtils.isNotBlank(names)) {
            String[] nameArr = names.split("[§]");
            for (String element : nameArr) {
                if (org.apache.commons.lang3.StringUtils.isNotBlank(element)) {
                    ReviewUser reviewUser = new ReviewUser();
                    reviewUser.setReviewId(review.getId());
                    User user = new User();
                    user.setName(element);
                    reviewUser.setUser(user);
                    reviewUser.setId(IdGen.uuid());
                    reviewUserDao.insert(reviewUser);
                }
            }
        }

    }

    @Override
    @Transactional(readOnly = false)
    public void delete(Review review) {
        super.delete(review);
    }

    public Map<String, String> findListSum(Review review) {
        return reviewDao.findListSum(review);
    }

}